package com.rocksea.app.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mor.dataacquisition.DataAcquisition;
import com.mor.dataacquisition.net.DataCallBack;
import com.mor.dataacquisition.net.parsedData.ResponseData;
import com.rocksea.app.Account;
import com.rocksea.app.R;
import com.rocksea.app.dialog.CommitInspectionDialog;
import com.rocksea.app.entry.InspectionForm;
import com.rocksea.app.utils.HttpMessage;
import com.rocksea.app.utils.UiUtils;
import com.rocksea.app.weight.DividerListItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mrw on 2017/8/8.
 *
 */

public class InspeFormItemFragment extends Fragment {


    private InspectionForm.InspectionFormListBean mFormListBean;

    public static InspeFormItemFragment newInstance(Bundle args) {
        InspeFormItemFragment fragment = new InspeFormItemFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mBootView  = inflater.inflate(R.layout.fragment_inpse_from_activity_v1,container,false);
        Bundle mBundle = getArguments();
        mFormListBean = mBundle.getParcelable(UiUtils.BUNDLE_KEY);
        ArrayList<String> mInfos = init(mFormListBean);
        RecyclerView mListView = (RecyclerView) mBootView.findViewById(R.id.List_View);
        //new InspcItemAdpter(getContext(), mInfos,R.layout.inspe_list_item)
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        // 设置布局管理器
        mListView.setLayoutManager(layoutManager); //不添加这句 不显示
        mListView.setAdapter(new AdapterDemo(mInfos,getContext()));
        mListView.addItemDecoration(new DividerListItemDecoration(getContext(),DividerListItemDecoration.VERTICAL_LIST));
        return mBootView;
    }
    private ArrayList<String> init(InspectionForm.InspectionFormListBean mFormListBean){
        ArrayList<String> mInfo = new ArrayList<>();
        mInfo.add(String.format("%s|%s","报检单编号",mFormListBean.number.equals("")?"未知":mFormListBean.number));
        mInfo.add(String.format("%s|%s","建筑物名称",mFormListBean.siteName.equals("")?"未知":mFormListBean.structName));
        mInfo.add(String.format("%s|%s","标段名称",mFormListBean.sectName.equals("")?"未知":mFormListBean.sectName));
        if (mFormListBean.pierNum.contains("#"))
        {
            mInfo.add(String.format("%s|%s","墩号",mFormListBean.pierNum));
        }
        else
        {
            mInfo.add(String.format("%s|%s","墩号", String.format("%s#",mFormListBean.pierNum)));
        }

        switch (mFormListBean.applyMethod)
        {
            case "1":
                mInfo.add(String.format("%s|%s","报检检测方法","低应变"));
                break;
            case "2":
                mInfo.add(String.format("%s|%s","报检检测方法","高应变"));
                break;
            case "3":
                mInfo.add(String.format("%s|%s","报检检测方法","声测"));
                break;
            case "4":
                mInfo.add(String.format("%s|%s","报检检测方法","静载"));
                break;
            default:
                mInfo.add(String.format("%s|%s","报检检测方法","未知"));
                break;
        }
        mInfo.add(String.format("%s|%s","受理人",mFormListBean.receiver.equals("")?"未知":mFormListBean.receiver));
        mInfo.add(String.format("%s|%s","申请人",mFormListBean.applicantName.equals("")?"未知":mFormListBean.applicantName));
        mInfo.add(String.format("%s|%s","申请人电话",mFormListBean.applicantTel.equals("")?"未知":mFormListBean.applicantTel));
        mInfo.add(String.format("%s|%s","报检时间",mFormListBean.applyTime.equals("")?"未知":mFormListBean.applyTime));
        mInfo.add(String.format("%s|%s","混凝土强度等级",mFormListBean.concreteStrength.equals("")?"未知":"C"+mFormListBean.concreteStrength));
        mInfo.add(String.format("%s|%s","设计桩长",mFormListBean.desPileLength.equals("")?"未知":mFormListBean.desPileLength+"m"));
        mInfo.add(String.format("%s|%s","设计桩径",mFormListBean.pileDiameter.equals("")?"未知":mFormListBean.pileDiameter+"m"));
        //mInfo.add(String.format("%s|%s","成孔方式",mFormListBean.holeMethod.equals("")?"未知":mFormListBean.holeMethod));

        mInfo.add(String.format("%s|%s","里程",mFormListBean.centerMile.equals("")?"未知":mFormListBean.centerMile));
        mInfo.add(String.format("%s|%s","受力类型",mFormListBean.stressType.equals("")?"未知":mFormListBean.stressType));
        return mInfo;
    }

    private static class AdapterDemo extends RecyclerView.Adapter<AdapterDemo.ViewHolder> {

        private ArrayList<String> datas;
        private Context mContext;
        private AdapterDemo(ArrayList<String> datas,Context mContext) {
            this.datas = datas;
            this.mContext = mContext;
        }

        @Override
        public AdapterDemo.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View item = LayoutInflater.from(mContext).inflate(R.layout.inspe_list_item, parent,false);
            return new ViewHolder(item);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            String index = datas.get(position);
            if (index.contains("|"))
            {
                String[] infos = index.split("\\|");
                holder.mInfo.setText(infos[1]);
                holder.mTitle.setText(infos[0]);
                holder.mInfo.setTextColor(Color.BLACK);
            }
        }
        @Override
        public int getItemCount() {
            return datas.size();
        }

        public static class ViewHolder extends RecyclerView.ViewHolder {
            public TextView mTitle, mInfo;
            public ViewHolder(View view)
            {
                super(view);
                mTitle = (TextView) view.findViewById(R.id.Title);
                mInfo = (TextView) view.findViewById(R.id.Info);
            }
        }
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);  //注册菜单

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {
        inflater.inflate(R.menu.optionfrom_menu,menu);
        super.onCreateOptionsMenu(menu,inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.Trun_Down:
                commitInspection();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private DataCallBack<ResponseData> dataCallBack = new DataCallBack<ResponseData>()
    {
        @Override
        public void processData(ResponseData data)
        {
            int code = data.returnCode;
            Log.e("optionCallBack", "processData: "+data.returnCode);
            if (code==0)
            {
                UiUtils.showToast("操作成功");
            }
            else if (code==-90)
            {
                UiUtils.showToast("网络异常,未能连接到服务器");
            }
            else if (code==-98){
                UiUtils.showToast("重复操作");
            }
            else if (code == -5)
            {
                UiUtils.showToast("已检报检单无法全部驳回");
            }
            else
            {
                UiUtils.showToast(String.format("操作失败,服务器异常(%s)",
                        HttpMessage.getErrorInfo(HttpMessage.commitInspectionForm,data.returnCode)));
            }
        }
    };

    private void commitInspection()
    {
        CommitInspectionDialog dialog = new CommitInspectionDialog(getContext(),mFormListBean);
        dialog.setContentView(R.layout.commit_dialog_layout);
        dialog.setOnCommitInspectionListener(new CommitInspectionDialog.OnCommitInspectionListener()
        {
            @Override
            public void onCommitInspectionListener(int actionMode, List<Integer> choosePosition,String remake,int counts,String errorMessage)
            {
                Log.i("InspectionListener: ",actionMode+"");
                int rejectTurn = 0;
                ArrayList<Long> pileIds = new ArrayList<>();
                switch (actionMode)
                {
                    case CommitInspectionDialog.MODE_PASS:
                        rejectTurn = 0;
                        for (int i = 0; i < choosePosition.size(); i++) {
                            String id = mFormListBean.pileList.get(choosePosition.get(i)).pileId;
                            pileIds.add(Long.valueOf(id));
                        }
                        break;
                    case CommitInspectionDialog.MODE_ALLUNPASS:
                        rejectTurn = 2;
                        for (int i = 0; i < choosePosition.size(); i++) {
                            String id = mFormListBean.pileList.get(choosePosition.get(i)).pileId;
                            pileIds.add(Long.valueOf(id));
                        }
                        break;
                    case CommitInspectionDialog.MODE_OVERPASS:
                        rejectTurn = 1;
                        for (int i = 0; i < choosePosition.size(); i++) {
                            String id = mFormListBean.pileList.get(choosePosition.get(i)).pileId;
                            pileIds.add(Long.valueOf(id));
                        }
                        break;
                }
                long[] ids = new long[pileIds.size()];
                for (int i = 0; i < pileIds.size(); i++)
                {
                    ids[i] = pileIds.get(i);
                }
                if (errorMessage == null || TextUtils.isEmpty(errorMessage))
                    errorMessage = "正常";
                if (remake == null || TextUtils.isEmpty(remake))
                    remake = "正常";
                DataAcquisition.getInstance().commitInspectionForm(Account.getRandomCode(),Account.getAccount(),
                        mFormListBean.id,mFormListBean.number,counts,remake,rejectTurn,ids,errorMessage,getContext(),dataCallBack);
            }
        });
        dialog.show();
    }
}
